﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DigDes.DSchool.SUPS.DataAccess.Interface;

namespace DigDes.DSchool.SUPS.DataAccess.Database
{
    internal class FileLoaderDACWeb
    {
        private static readonly string ConnectionStringKey = "SUPSDB";

        public List<LoaderStringDTO> SelectAllFromDB()
        {
            var connectionString = ConfigurationManager.ConnectionStrings[ConnectionStringKey].ConnectionString;
            List<LoaderStringDTO> data = new List<LoaderStringDTO>();
            using (SqlConnection sqlConn = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand("SelectAll", sqlConn);
                try
                {
                    sqlConn.Open();
                    SqlDataReader reader = command.ExecuteReader();

                    while (reader.Read())
                    {
                        int index = 0;
                        LoaderStringDTO row = new LoaderStringDTO();


                        row.CarriageNumber = reader.GetString(index++);
                        row.ContentWeight = reader.GetInt32(index++);
                        row.DestinationStation = reader.GetString(index++);
                        row.ContentCode = reader.GetString(index++);
                        row.ConsigneeСode = reader.GetString(index++);
                        row.CarriageParkType = reader.GetString(index++);
                        row.StartStation = reader.GetString(index++);
                        row.OperationCode = reader.GetString(index++);
                        row.OperationDate = reader.GetString(index++);
                        row.OperationYear = reader.GetString(index++);
                        row.OperationTime = reader.GetString(index++);
                        row.OperationStation = reader.GetString(index++);
                        row.PuttingRoad = reader.GetString(index++);
                        row.ReceptionRoad = reader.GetString(index++);
                        row.TrainIndex = reader.GetString(index++);
                        row.TrainNumber = reader.GetInt32(index++);

                        data.Add(row);
                    }
                    reader.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                return data;
            }
        }
    }
}